import React, { useEffect, useState, useRef } from 'react';
import {  message, Input, Select, Form, Button} from 'antd';
import { doctorAllGet,AddGet} from '../../../request/xxApi';
import AddDepart from '../../../styles/AddDepartment.module.css'



function AddDepartment() {
  // 初始化医生列表状态
  const [doctorAll, setDoctorAll] = useState([]);
  useEffect(() => {
    doctorAllGet().then((res) => {
        const doctors = res.data.map((doctor) => ({
          label: doctor.name,
          value: doctor.name,
        }));
        setDoctorAll(doctors);
    })
  },[])

  // 初始化加载状态
  const [loading, setLoading] = useState(false);
  // 创建表单实例
  const [form] = Form.useForm();
  // 创建当前记录的引用
  const currentRecordRef = useRef(null);
  // 样式对象或变量
  const Department = {
    director_Select: 'director-select-class',
    director_btn_box: 'director-btn-box-class',
    modal_btn: 'modal-btn-class',
    modal_btnn: 'modal-btnn-class',
  };

  // 表单提交
  const onFormFinish = (values) => {
    AddGet({name:values.name,maindoctor:values.maindoctor}).then(res=>{
       if(res.code===1){
        message.success('成功添加');
        form.resetFields();
       }
    })
  };

  // 取消按钮
  const handleOkClick = () => {
  };

  // 表单布局函数
  const formLayout = 'horizontal'; 

  return (
    <div style={{padding:'10px 20px'}}>
      <Form
        onFinish={onFormFinish}
        layout={formLayout}
        form={form}
        style={{
          marginTop: '20px',
          maxWidth: formLayout === 'inline' ? 'none' : 600,
        }}
      >
        <Form.Item name="name" label="科室名称">
          <Input placeholder={currentRecordRef.current ? currentRecordRef.current.name : ''} />
        </Form.Item>
        <Form.Item name="maindoctor" label="科室主任">
          <Select placeholder="全部医生" className={Department.director_Select} options={doctorAll} />
        </Form.Item>
        <Form.Item name="createtime" label="创建时间">
          <Input placeholder='系统自动生成' disabled />
        </Form.Item>
        <Form.Item name="id" label="科室编号">
          <Input disabled placeholder='系统自动生成'/>
        </Form.Item>
        <Form.Item>
          <div className={Department.director_btn_box}>
            <Button className={AddDepart.left_btn} type="primary" htmlType="submit">
              确认
            </Button>
            <Button key="submit" className={Department.modal_btnn} loading={loading} onClick={handleOkClick}>
              取消
            </Button>
          </div>
        </Form.Item>
      </Form>
    </div>
  );
}

export default AddDepartment;